{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Pascal's Triangle II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #dynamic-programming"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #动态规划"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getRow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #杨辉三角 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个非负索引 <code>rowIndex</code>，返回「杨辉三角」的第 <code>rowIndex</code><em> </em>行。</p>\n",
    "\n",
    "<p><small>在「杨辉三角」中，每个数是它左上方和右上方的数的和。</small></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://pic.leetcode-cn.com/1626927345-DZmfxB-PascalTriangleAnimated2.gif\" /></p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> rowIndex = 3\n",
    "<strong>输出:</strong> [1,3,3,1]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> rowIndex = 0\n",
    "<strong>输出:</strong> [1]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> rowIndex = 1\n",
    "<strong>输出:</strong> [1,1]\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 <= rowIndex <= 33</code></li>\n",
    "</ul>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>进阶：</strong></p>\n",
    "\n",
    "<p>你可以优化你的算法到 <code><em>O</em>(<i>rowIndex</i>)</code> 空间复杂度吗？</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [pascals-triangle-ii](https://leetcode.cn/problems/pascals-triangle-ii/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [pascals-triangle-ii](https://leetcode.cn/problems/pascals-triangle-ii/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['3', '0', '1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:return [1]\n",
    "        res = [1, 1]\n",
    "        for i in range(2, rowIndex + 1):\n",
    "            nxt = [1] * (i+1)\n",
    "            for j in range(1, i):\n",
    "                nxt[j] = res[j-1] + res[j]\n",
    "            res = nxt\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex):\n",
    "        result = [1]\n",
    "\n",
    "        def jieceng(nums):\n",
    "            temp = 1\n",
    "            for i in range(1, nums + 1):\n",
    "                temp *= i\n",
    "            return temp\n",
    "\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            result.append(int(jieceng(rowIndex)/(jieceng(i) * jieceng(rowIndex - i))))\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ans=[1]\n",
    "        if rowIndex==0: return ans\n",
    "        for row in range(1, rowIndex+1):\n",
    "            dp=[0]*(row+1)\n",
    "            dp[0]=1\n",
    "            dp[-1]=1\n",
    "            for i in range(1, row):\n",
    "                dp[i]=ans[i-1]+ans[i]\n",
    "            ans=dp\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        if rowIndex == 1:\n",
    "            return [1, 1]\n",
    "        row = []\n",
    "        a = 1\n",
    "        for i in range(math.ceil((rowIndex + 1) / 2)):\n",
    "            row.append(int(a))\n",
    "            # 组合公式递推，已知第i项，则i+1项为“第i项*(n-i)/(i+1)”，第0项为1\n",
    "            a = (a * (rowIndex - i)) / (i + 1)\n",
    "        # 只计算前一半，后面一半是对称的\n",
    "        # row.extend(row[((rowIndex + 1) // 2 - 1)::-1])\n",
    "        for i in range((rowIndex + 1) // 2 - 1, -1, -1):\n",
    "            row.append(row[i])\n",
    "        return row\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "            \n",
    "        data1 = [1, 1]\n",
    "\n",
    "        for i in range(2, rowIndex+1):\n",
    "            data2 = [1]\n",
    "            for j in range(1, i):\n",
    "                data2.append(data1[j-1] + data1[j])\n",
    "            data1 = data2 + [1]\n",
    "        \n",
    "        return data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = [1]*(rowIndex+1)\n",
    "        if rowIndex<2:\n",
    "            return res\n",
    "        for i in range(rowIndex):\n",
    "            for j in range(i,0,-1):\n",
    "                res[j] = res[j] + res[j-1]\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = [1]\n",
    "        if rowIndex != 0:\n",
    "            for i in range(1, rowIndex):\n",
    "                res.append(res[i-1] * (rowIndex-i+1)//i)\n",
    "            res.append(1)\n",
    "        return res\n",
    "\n",
    "#0       1\n",
    "#1      1 1\n",
    "#2     1 2 1\n",
    "#3    1 3 3 1\n",
    "#4   1 4 6 4 1\n",
    "#5  1 5 10 10 5 1\n",
    "#6 1 6 15 20 15 6 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        row = [0] * (rowIndex+1)\n",
    "        for i in range(0,rowIndex+1):\n",
    "            for j in range(i, -1, -1):\n",
    "                if j == 0 or j == i:\n",
    "                    row[j] = 1\n",
    "                else:\n",
    "                    row[j] = row[j] + row[j-1]\n",
    "        return row\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex==0:\n",
    "            return [1]\n",
    "        if rowIndex==1:\n",
    "            return [1,1]\n",
    "        else:\n",
    "            res=[1]*(rowIndex+1)\n",
    "            for i in range(1,rowIndex+1,1):\n",
    "                for j in range(i-1,0,-1):\n",
    "                    res[j] += res[j-1]\n",
    "            return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        pre = [1]\n",
    "        for i in range(1, rowIndex+1):\n",
    "            curr = []\n",
    "            for j in range(i+1):\n",
    "                if (j == 0 or j == i):\n",
    "                    curr.append(1)\n",
    "                else:\n",
    "                    curr.append(pre[j-1] + pre[j])\n",
    "            pre = curr\n",
    "        return pre\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> list[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        if rowIndex == 1:\n",
    "            return [1, 1]\n",
    "        row = []\n",
    "        a = 1\n",
    "        for i in range(math.ceil((rowIndex + 1) / 2)):\n",
    "            row.append(int(a))\n",
    "            # 组合公式递推，已知第i项，则i+1项为“第i项*(n-i)/(i+1)”，第0项为1\n",
    "            a = (a * (rowIndex - i)) / (i + 1)\n",
    "        # 只计算前一半，后面一半是对称的\n",
    "        # row.extend(row[((rowIndex + 1) // 2 - 1)::-1])\n",
    "        for i in range((rowIndex + 1) // 2 - 1, -1, -1):\n",
    "            row.append(row[i])\n",
    "        return row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex==0:\n",
    "            return [1]\n",
    "        elif rowIndex==1:\n",
    "            return [1,1]\n",
    "        res=[]\n",
    "        for i in range(rowIndex):\n",
    "            res=[1]+[x+y for x,y in pairwise(res)]+[1]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        row = [1]\n",
    "        for _ in range(rowIndex): \n",
    "            row = [1] + [x + y for x, y in pairwise(row)] + [1]\n",
    "        return row\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        elif rowIndex == 1:\n",
    "            return [1,1]\n",
    "        prev = self.getRow(rowIndex-1)\n",
    "        ans = [1]\n",
    "        for i in range(len(prev)-1):\n",
    "            ans.append(prev[i]+prev[i+1])\n",
    "        ans.append(1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:     \n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        elif rowIndex == 1:\n",
    "            return [1,1]\n",
    "        else:\n",
    "            ln = self.getRow(rowIndex-1)\n",
    "            res = [1]\n",
    "            for i in range(1,rowIndex):\n",
    "                res.append(ln[i-1]+ln[i])\n",
    "            res.append(1)\n",
    "            return res\n",
    "        #     lna = [[1],[1,1]]\n",
    "        #     for j in range(2,rowIndex+1):\n",
    "        #         ln = [1]\n",
    "        #         for i in range(1,j):\n",
    "        #             ln.append(lna[j-1][i-1]+lna[j-1][i])\n",
    "        #         ln.append(1)\n",
    "        #         lna.append(ln)\n",
    "        #     return lna[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        a = [1]\n",
    "        for i in range(1,rowIndex+1):\n",
    "            a = list(map(lambda x, y: x + y, a+[0], [0]+a))\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ans = []\n",
    "        for i in range(rowIndex+1):\n",
    "            row = []\n",
    "            for j in range(0,i+1):\n",
    "                if j == 0 or j == i:\n",
    "                    row.append(1)\n",
    "                else:\n",
    "                    row.append(ans[i-1][j]+ans[i-1][j-1])\n",
    "            ans.append(row)\n",
    "        return row\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        def f(n,m):\n",
    "            return factorial(n)//factorial(m)//factorial(n-m)\n",
    "        n=rowIndex\n",
    "        lst=[1]*(n+1)\n",
    "        if n<2:\n",
    "            return lst\n",
    "        for i in range(n+1):\n",
    "            lst[i]=f(n,i)\n",
    "        return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ans = [1]\n",
    "        for i in range(rowIndex):\n",
    "            ans = [1] + [x + y for x, y in pairwise(ans)] + [1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        \"\"\"\n",
    "        输入: numRows = 5\n",
    "        输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]\n",
    "                                1\n",
    "                               1 1\n",
    "                              1 2 1\n",
    "                             1 3 3 1\n",
    "                            1 4 6 4 1\n",
    "                   2 res[2][1] = res[1][0] + res[1][1]\n",
    "\n",
    "                   3 res[3][1] = res[2][0] + res[2][1]\n",
    "                   3 res[3][2] = res[2][1] + res[1][2]\n",
    "\n",
    "                   4 res[4][1] = res[3][0] + res[3][1]\n",
    "                   6 res[4][2] = res[3][1] + res[3][2]\n",
    "                   4 res[4][3] = res[3][2] + res[3][3]\n",
    "                规律：res[n][i] = res[n-1][i-1] + res[n-1][i]\n",
    "        \"\"\"\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        if rowIndex == 1:\n",
    "            return [1, 1]\n",
    "\n",
    "        # 数据初始化\n",
    "        res = [[1], [1, 1]] + [[1] + [0] * (i - 1) + [1] for i in range(2, rowIndex+1)]\n",
    "\n",
    "        for i in range(2, rowIndex + 1):\n",
    "            # res[n][i] = res[n-1][i-1] + res[n-1][i]\n",
    "            for j in range(1, len(res[i]) - 1):\n",
    "                res[i][j] = res[i - 1][j - 1] + res[i - 1][j]\n",
    "        return res[rowIndex]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res=[[1],[1,1]]\n",
    "        if rowIndex==0:\n",
    "            return res[0]\n",
    "        elif rowIndex==1:\n",
    "            return res[1]\n",
    "        else:\n",
    "            for i in range(rowIndex-1):\n",
    "                temp=[]\n",
    "                for j in range(len(res[-1])-1):\n",
    "                    temp.append(res[-1][j]+res[-1][j+1])\n",
    "                temp=[1]+temp+[1]\n",
    "                res.append(temp)\n",
    "        return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        row = [1]\n",
    "        for _ in range(rowIndex): \n",
    "            row = [1] + [x + y for x, y in pairwise(row)] + [1]\n",
    "        return row\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        \n",
    "        res = []       \n",
    "        l = 1\n",
    "        for i in range(1, rowIndex + 2):\n",
    "            if l == 1:\n",
    "                res.append([1])\n",
    "            else:\n",
    "                lis = [1]\n",
    "                for j in range(1, l-1):\n",
    "                    lis.append((res[i-2][j-1] + res[i-2][j]))  \n",
    "                lis.append(1)              \n",
    "                res.append(lis) \n",
    "            l += 1\n",
    "\n",
    "        return res[i - 1]        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ans = [0] * (rowIndex+1)\n",
    "        for i in range(rowIndex + 1):\n",
    "            for j in range(i, -1, -1):\n",
    "                if j == 0 or j == i:\n",
    "                    ans[j] = 1\n",
    "                else:\n",
    "                    ans[j] = ans[j-1] + ans[j]\n",
    "            # print(ans)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        res = [[1]]\n",
    "        for i in range(1, rowIndex+1):\n",
    "            level = [1]\n",
    "            for j in range(1, i):\n",
    "                level.append(res[-1][j-1] + res[-1][j])\n",
    "            level.append(1)\n",
    "            res.append(level)\n",
    "        return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        \"\"\"\n",
    "        输入: numRows = 5\n",
    "        输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]\n",
    "                                1\n",
    "                               1 1\n",
    "                              1 2 1\n",
    "                             1 3 3 1\n",
    "                            1 4 6 4 1\n",
    "                   2 res[2][1] = res[1][0] + res[1][1]\n",
    "\n",
    "                   3 res[3][1] = res[2][0] + res[2][1]\n",
    "                   3 res[3][2] = res[2][1] + res[1][2]\n",
    "\n",
    "                   4 res[4][1] = res[3][0] + res[3][1]\n",
    "                   6 res[4][2] = res[3][1] + res[3][2]\n",
    "                   4 res[4][3] = res[3][2] + res[3][3]\n",
    "                规律：res[n][i] = res[n-1][i-1] + res[n-1][i]\n",
    "        \"\"\"\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        if rowIndex == 1:\n",
    "            return [1, 1]\n",
    "\n",
    "        # 数据初始化\n",
    "        res = [[1, 1], []]\n",
    "\n",
    "        for i in range(2, rowIndex + 1):\n",
    "            # res[n][i] = res[n-1][i-1] + res[n-1][i]\n",
    "            for j in range(1, len(res[0])):\n",
    "                res[1].append(res[0][j - 1] + res[0][j])\n",
    "            res[1].insert(0, 1)\n",
    "            res[1].append(1)\n",
    "            res[0].clear()\n",
    "            res[0] += res[1]\n",
    "            res[1].clear()\n",
    "        return res[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex==0:return [1]\n",
    "        else:\n",
    "            ans=self.getRow(rowIndex-1)\n",
    "            new_ans=[1]+[ans[i]+ans[i+1] for i in range(len(ans)-1)]+[1]\n",
    "            return new_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex==0:\n",
    "            return [1]\n",
    "        if rowIndex==1:\n",
    "            return [1,1]\n",
    "        rst=[1]\n",
    "        temp_lst=self.getRow(rowIndex-1)\n",
    "        for idx in range(rowIndex-1):\n",
    "            rst.append(temp_lst[idx]+temp_lst[idx+1])\n",
    "        rst.append(1)\n",
    "        return rst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "##    def getRow(self, rowIndex: int) -> List[int]:\n",
    "    def getRow(self, rowIndex) :\n",
    "        res=[[1]*(i+1) for i in range(rowIndex+1)]\n",
    "\n",
    "        if rowIndex==0:\n",
    "            return [1]\n",
    "\n",
    "        for i in range(2,rowIndex+1):\n",
    "            for j in range(1,i):\n",
    "                res[i][j]=res[i-1][j-1]+res[i-1][j]\n",
    "\n",
    "        return res[rowIndex]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        f = [1,1]\n",
    "        s = []\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        for i in range(rowIndex-1):\n",
    "            s.append(1)\n",
    "            for j in range(len(f) - 1):\n",
    "                s.append(f[j] + f[j + 1])\n",
    "            s.append(1)\n",
    "            f = s[:]\n",
    "            s = []\n",
    "        return f\n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        r = [1]\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            r.insert(0, 0)\n",
    "            for j in range(i):\n",
    "                r[j] = r[j] + r[j + 1]\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        YHTriangle = []\n",
    "        YHTriangle.append([1])\n",
    "        YHTriangle.append([1,1])\n",
    "        for row in range(3,rowIndex+2):\n",
    "            newRow = [1]\n",
    "            oldRow = YHTriangle[row-2]\n",
    "            for i in range(len(oldRow)-1):\n",
    "                newNum= oldRow[i]+oldRow[i+1]\n",
    "                newRow.append(newNum)\n",
    "            newRow.append(1)\n",
    "            YHTriangle.append(newRow)\n",
    "        return YHTriangle[rowIndex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, n: int) -> List[int]:\n",
    "       dp = [1 for i in range(n+1)]\n",
    "       if n<2:\n",
    "           return dp\n",
    "       for i in range(2,n+1):\n",
    "           for j in range(i-1,0,-1):\n",
    "               dp[j] = dp[j-1]+dp[j]\n",
    "       return dp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        numRows = rowIndex+1\n",
    "        dp = []\n",
    "        for i in range(1,numRows+1):\n",
    "            dp_i = []\n",
    "            for j in range(i):\n",
    "                dp_i.append(1)\n",
    "            dp.append(dp_i)\n",
    "        \n",
    "        for i in range(1,numRows):\n",
    "            for j in range(1,i):\n",
    "                # dp[i][j] = max(dp[i-1][j-1]+dp[i-1][j], 1)\n",
    "                dp[i][j] = dp[i-1][j-1]+dp[i-1][j]\n",
    "\n",
    "        return dp[rowIndex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res =[[1]]\n",
    "        rowindex = rowIndex\n",
    "        while rowindex>=1:\n",
    "            newrow = [a+b for a,b in zip([0]+res[-1],res[-1]+[0])]\n",
    "            res.append(newrow)\n",
    "            rowindex -= 1\n",
    "\n",
    "        return res[rowIndex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        # f=[[1]*(x+1) for x in range(rowIndex+1)]\n",
    "        # if rowIndex<2:\n",
    "        #     return f[rowIndex]\n",
    "        # for i in range(2,rowIndex+1):\n",
    "        #     for j in range(1,i):\n",
    "        #         f[i][j]=f[i-1][j]+f[i-1][j-1]\n",
    "        # return f[rowIndex]\n",
    "        f=[1]*(rowIndex+1)\n",
    "        for i in range(2,rowIndex+1):\n",
    "            for j in range(i-1,0,-1):\n",
    "                f[j]+=f[j-1]\n",
    "        return f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        row_1 = [1]\n",
    "        row_2 = [1]\n",
    "        row = [1]\n",
    "        for i in range(rowIndex):\n",
    "            row.clear()\n",
    "            row_1.insert(0,0)\n",
    "            row_2.insert(len(row_2),0)\n",
    "            for j in range(len(row_1)):\n",
    "                row.append(row_1[j] + row_2[j])\n",
    "            row_1.clear()\n",
    "            row_2.clear()\n",
    "            row_1.extend(row)\n",
    "            row_2.extend(row)\n",
    "        return row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        pre_row = []\n",
    "        if rowIndex >=0:\n",
    "            pre_row = [1]\n",
    "\n",
    "            current_row = []\n",
    "            for i in range(1,rowIndex+1):\n",
    "                for j in range(0,i+1):\n",
    "                    l = 0\n",
    "                    r = 0\n",
    "                    if j-1 >=0:\n",
    "                        l = pre_row[j-1]\n",
    "                    if j < len(pre_row):\n",
    "                        r = pre_row[j]\n",
    "                    node = l+r\n",
    "                    current_row.append(node)\n",
    "                pre_row = current_row\n",
    "                current_row = []\n",
    "        \n",
    "        if current_row:\n",
    "            return current_row\n",
    "        else:\n",
    "            return pre_row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        rowIndex = rowIndex + 1\n",
    "        list_yh_row = []\n",
    "        # for i in range(numRows):\n",
    "        #     list_yh.append([])\n",
    "        # print(list_yh)\n",
    "        # for i in range(1,numRows+1):\n",
    "        #     for j in range(i):\n",
    "        #         if j==0 or j==i-1:\n",
    "        #             list_yh[i-1].append(1)\n",
    "        #         else:\n",
    "        #             list_yh[i-1].append(list_yh[i-2][j-1]+list_yh[i-2][j])\n",
    "        # print(list_yh)\n",
    "        # def fun(i):\n",
    "        #     if i==\n",
    "        for i in range(rowIndex):\n",
    "            list_yh_row.append([])\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            for j in range(i):\n",
    "                if j == 0 or j == i - 1:\n",
    "                    list_yh_row[i - 1].append(1)\n",
    "                else:\n",
    "                    list_yh_row[i - 1].append(list_yh_row[i - 2][j - 1] + list_yh_row[i - 2][j])\n",
    "        # print(list_yh_row[-1])\n",
    "        # print(list_yh_row)\n",
    "        return list_yh_row[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, n: int) -> List[int]:\n",
    "        b = [1]\n",
    "        for i in range(n):\n",
    "            a = [1]\n",
    "            for j in range(1,len(b)):\n",
    "                s = b[j]+b[j-1]\n",
    "                a.append(s)\n",
    "            a.append(1)\n",
    "            b = a\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(rowIndex + 1):\n",
    "            for j in range(i - 1, 0, -1):\n",
    "                res[j] = res[j - 1] + res[j]\n",
    "            res.append(1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        if rowIndex == 1:\n",
    "            return [1,1]\n",
    "        dp = [[0] * (n+1) for n in range(rowIndex+1)]\n",
    "        for i in range(rowIndex+1):\n",
    "            if i == 0:\n",
    "                dp[i][0] = 1\n",
    "            else:\n",
    "                dp[i][0] = 1\n",
    "                dp[i][i] = 1\n",
    "        for i in range(2,rowIndex+1):\n",
    "            for j in range(1, i):\n",
    "                dp[i][j] = dp[i-1][j-1] + dp[i-1][j]\n",
    "        return dp[rowIndex]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        \n",
    "        a=[]\n",
    "        for i in range(rowIndex+1):\n",
    "            a.append([])\n",
    "            for j in range(i+1):\n",
    "                if j==0:\n",
    "                    a[i].append(1)\n",
    "                elif i==j:\n",
    "                    a[i].append(1)\n",
    "                else:\n",
    "                    a[i].append(a[i-1][j-1]+a[i-1][j]) \n",
    "        # a.split(',')\n",
    "        return a[rowIndex]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        \n",
    "        ans = []\n",
    "        for i in range(rowIndex + 1):\n",
    "            ans.append(math.factorial(rowIndex) // (math.factorial(i) * math.factorial(rowIndex - i)))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:return [1]\n",
    "        if rowIndex == 1:return [1,1]\n",
    "        if rowIndex == 2:return [1,2,1]\n",
    "        rowIndex += 1\n",
    "        ans = [1,2,1]\n",
    "        for i in range(3, rowIndex):\n",
    "            v = []\n",
    "            for j in range(1,i):\n",
    "                v.append(ans[j]+ans[j-1])\n",
    "            ans[:] = [1] + v[:] + [1]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        # 创建dp数组\n",
    "        triangle = []\n",
    "        for i in range(rowIndex+1):\n",
    "            if i <= 1:\n",
    "                row = [1]*(i+1)\n",
    "            else:\n",
    "                row = [1]*(i+1)\n",
    "                for j in range(1, i):\n",
    "                    row[j] = triangle[i-1][j-1]+triangle[i-1][j]\n",
    "            triangle.append(row)\n",
    "        return triangle[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        def fact(n):\n",
    "            if n == 0:\n",
    "                return 1\n",
    "            else:\n",
    "                return n*fact(n-1)\n",
    "        def C(n,m):\n",
    "            return fact(n)//fact(m)//fact(n-m)\n",
    "        a=[]\n",
    "        for i in range(rowIndex+1):\n",
    "            a.append(C(rowIndex,i))\n",
    "        return a\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ans=list()\n",
    "        # if rowIndex<=1:\n",
    "        #     ans.append(1)\n",
    "        #     return ans\n",
    "        # else:\n",
    "\n",
    "        #     for i in range(rowIndex):\n",
    "        #         ans.insert(i,math.factorial(rowIndex-1)/(math.factorial(i)*math.factorial(rowIndex-1-i)))\n",
    "        # return ans\n",
    "        ans.append(1)\n",
    "        for i in range(1,rowIndex+1):\n",
    "            ans.insert(i,int((rowIndex+1-i)*ans[i-1]/i))\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        r = [1]\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            r.insert(0, 0)\n",
    "            for j in range(i):\n",
    "                r[j] = r[j] + r[j+1]\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        list = [1] * (rowIndex + 1)\n",
    "        if rowIndex <= 1:\n",
    "            return list\n",
    "        for i in range(2,rowIndex + 1):\n",
    "            for j in range(i - 1, 0, -1):\n",
    "                list[j] = list[j - 1] + list[j]\n",
    "\n",
    "        return list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "\n",
    "        triangle = [[1]]\n",
    "\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            row = [1]\n",
    "\n",
    "            for j in range(1, i):\n",
    "                row.append(triangle[i-1][j-1] + triangle[i-1][j])\n",
    "\n",
    "            row.append(1)\n",
    "            triangle.append(row)\n",
    "\n",
    "        return triangle[rowIndex]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "\n",
    "        dp = [[1]]\n",
    "        def get_num(dp):\n",
    "            tmp = []\n",
    "            for i in range(len(dp)-1):\n",
    "                tmp.append(dp[i]+dp[i+1])\n",
    "            return [1]+tmp+[1]\n",
    "\n",
    "        i=0\n",
    "        while i<rowIndex:\n",
    "            dp.append(get_num(dp[-1]))\n",
    "            i+=1\n",
    "        return dp[rowIndex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(rowIndex+1):\n",
    "            row = []\n",
    "            for j in range(0 , i+1):\n",
    "                if j == 0 or j == i:\n",
    "                    row.append(1)\n",
    "                else:\n",
    "                    row.append(res[i-1][j-1] + res[i-1][j])\n",
    "            res.append(row)\n",
    "        return row\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        else:\n",
    "            res = [[1],[1,1]]\n",
    "            if rowIndex == 1:\n",
    "                return res[-1]\n",
    "            row = [1,1]\n",
    "            for i in range(rowIndex-1):\n",
    "                last_row = row\n",
    "                row = [1]\n",
    "                for j in range(len(last_row)-1):\n",
    "                    row.append(last_row[j] + last_row[j+1])\n",
    "                row.append(1)\n",
    "                res.append(row)\n",
    "            return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = [1]*(rowIndex+1)\n",
    "        if rowIndex < 2:\n",
    "            return res\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            for j in range(i, -1, -1):\n",
    "                if j != 0 and j != i:\n",
    "                    res[j] = res[j] + res[j-1]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        list = [1] * (rowIndex + 1)\n",
    "        if rowIndex <= 1:\n",
    "            return list\n",
    "        for i in range(2,rowIndex + 1):\n",
    "            for j in range(i - 1, 0, -1):\n",
    "                list[j] = list[j - 1] + list[j]\n",
    "\n",
    "        return list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:    \n",
    "        triangle = []\n",
    "        for i in range(rowIndex+1):\n",
    "            row = []\n",
    "            for j in range(i + 1):\n",
    "                if j == 0 or j == i:\n",
    "                    row.append(1)\n",
    "                else:\n",
    "                    row.append(triangle[i - 1][j - 1] + triangle[i - 1][j])\n",
    "            triangle.append(row)\n",
    "            b=rowIndex+1\n",
    "        return triangle[b-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ls = [[1] * (i+1) for i in range(rowIndex+1)]\n",
    "        for i in range(2, rowIndex+1):\n",
    "            for j in range(1, len(ls[i])-1):\n",
    "                ls[i][j] = ls[i-1][j-1] + ls[i-1][j]\n",
    "        return ls[rowIndex]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        if rowIndex == 0:\n",
    "            return [1]\n",
    "        if rowIndex == 1:\n",
    "            return [1,1]\n",
    "        res = []\n",
    "        res.append(1)\n",
    "        pre_row = self.getRow(rowIndex - 1)\n",
    "        # for i,num in enumerate(pre_row):\n",
    "        #     res.append()\n",
    "        for i in range(len(pre_row)-1):\n",
    "            res.append(pre_row[i]+pre_row[i+1])\n",
    "        res.append(1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(rowIndex+1):\n",
    "            row = []\n",
    "            for j in range(0 , i+1):\n",
    "                if j == 0 or j == i:\n",
    "                    row.append(1)\n",
    "                else:\n",
    "                    row.append(res[i-1][j-1] + res[i-1][j])\n",
    "            res.append(row)\n",
    "        return row\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        row = [1]\n",
    "        for _ in range(rowIndex):\n",
    "            row = [1] + [x + y for x, y in pairwise(row)] + [1]\n",
    "        return row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res = [[1]*i for i in range(1,rowIndex+2)]\n",
    "        if rowIndex<2:\n",
    "            return res[rowIndex]\n",
    "        \n",
    "        for i in range(2,rowIndex+2):\n",
    "            for j in range(1,len(res[i-1])-1):\n",
    "                res[i-1][j] = res[i-2][j-1]+res[i-2][j]\n",
    "        \n",
    "        return res[rowIndex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:    \n",
    "        triangle = []\n",
    "        for i in range(rowIndex+1):\n",
    "            row = []\n",
    "            for j in range(i + 1):\n",
    "                if j == 0 or j == i:\n",
    "                    row.append(1)\n",
    "                else:\n",
    "                    row.append(triangle[i - 1][j - 1] + triangle[i - 1][j])\n",
    "            triangle.append(row)\n",
    "            b=rowIndex+1\n",
    "        return triangle[b-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        res=[1]\n",
    "        if  rowIndex ==0 :\n",
    "            return res\n",
    "        for i in range(1,rowIndex+2):\n",
    "            temp1=[0]+res\n",
    "            temp2=res+[0]\n",
    "            res=[(temp1[j]+temp2[j]) for j in range(i)]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        ret = []\n",
    "        for i in range(rowIndex + 1):\n",
    "            row = [1] * (i + 1)\n",
    "            for j in range(1, len(row) - 1):\n",
    "                row[j] = ret[i - 1][j - 1] + ret[i - 1][j]\n",
    "            ret.append(row)\n",
    "\n",
    "        return ret[rowIndex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        row = [0] * (rowIndex + 1)\n",
    "        row[0] = 1\n",
    "\n",
    "        for i in range(1, rowIndex + 1):\n",
    "            for j in range(i, 0, -1):\n",
    "                row[j] += row[j - 1]\n",
    "\n",
    "        return row\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> list[int]:\n",
    "        YH = [1]*(rowIndex+1)\n",
    "        for i in range(0,rowIndex+1):\n",
    "            for j in range(i,1,-1):\n",
    "                YH[j-1] = YH[j-1] + YH[j-2]\n",
    "        return YH"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        # dp = [[1 for i in range(j+1)] for j in range(rowIndex+1)]\n",
    "        # for i in range(rowIndex):\n",
    "        #     if i>=2:\n",
    "        #         for j in range(1,i):\n",
    "        #             dp[i][j] = dp[i-1][j-1]+dp[i-1][j]\n",
    "        #             return dp\n",
    "        dp = []\n",
    "        for i in range(rowIndex+1):\n",
    "            rows = [1]*(i+1)\n",
    "            if i >= 2:\n",
    "                for j in range(1, i):\n",
    "                    rows[j] = dp[i-1][j] + dp[i-1][j-1]\n",
    "            dp.append(rows)\n",
    "        return dp[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        per_list= []\n",
    "        next_list = []\n",
    "        for i in range(1, rowIndex + 2):\n",
    "            if i == 1:\n",
    "                next_list = [1]\n",
    "            elif i == 2:\n",
    "                next_list = [1, 1]\n",
    "            else:\n",
    "                next_list = [1]\n",
    "                for j in range(len(per_list) - 1):\n",
    "                    next_list.append(per_list[j] + per_list[j + 1])\n",
    "                next_list.append(1)\n",
    "            per_list = next_list\n",
    "        return next_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        pre_row = []\n",
    "        if rowIndex >=0:\n",
    "            pre_row = [1]\n",
    "\n",
    "            current_row = []\n",
    "            for i in range(1,rowIndex+1):\n",
    "                for j in range(0,i+1):\n",
    "                    l = 0\n",
    "                    r = 0\n",
    "                    if j-1 >=0:\n",
    "                        l = pre_row[j-1]\n",
    "                    if j < len(pre_row):\n",
    "                        r = pre_row[j]\n",
    "                    node = l+r\n",
    "                    current_row.append(node)\n",
    "                pre_row = current_row\n",
    "                current_row = []\n",
    "        \n",
    "        if current_row:\n",
    "            return current_row\n",
    "        else:\n",
    "            return pre_row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getRow(self, rowIndex: int) -> List[int]:\n",
    "        numRows = rowIndex + 1\n",
    "        ans = []\n",
    "        if numRows == 0:\n",
    "            return ans\n",
    "        elif numRows == 1:\n",
    "            ans = [1]\n",
    "            return ans\n",
    "        elif numRows == 2:\n",
    "            ans = [1,1]\n",
    "            return ans\n",
    "        else:\n",
    "            init_temp = [1, 1]\n",
    "            temp = [1]\n",
    "            numRows = numRows - 2\n",
    "            for i in range(numRows):\n",
    "                j = 0\n",
    "                while j < len(init_temp) - 1:\n",
    "                    temp.append(init_temp[j] + init_temp[j + 1])\n",
    "                    j += 1\n",
    "                temp.append(1)\n",
    "                init_temp = temp\n",
    "                ans.append(temp)\n",
    "                temp = [1]\n",
    "            return init_temp"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
